package com.wardrobe.soa.backend.booking;

import com.google.common.collect.Maps;
import com.wardrobe.domain.booking.BookingBean;
import com.wardrobe.domain.booking.BookingFromBean;
import com.wardrobe.domain.booking.BookingShowBean;
import com.wardrobe.domain.common.BackEndFromBean;
import com.wardrobe.domain.goods.GoodsBean;
import com.wardrobe.domain.goods.GoodsFromBean;
import com.wardrobe.domain.goods.GoodsShowBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/16.
 */
@RestController
@RequestMapping("/backend/booking")
public class BackBookingController {
    private static final Logger logger = LoggerFactory.getLogger(BackBookingController.class);

    @Autowired
    private BackBookingService backBookingService;

    @RequestMapping("/getBookingList")
    public Map<String, Object> getBookingList(BookingFromBean fromBean) {
        Map<String, Object> result = Maps.newHashMap();
        List<BookingShowBean> list = backBookingService.getBookingList(fromBean);
        list.stream().forEach(bookingShowBean -> {
            bookingShowBean.setCreateTime(bookingShowBean.getCreateTime().substring(0, bookingShowBean.getCreateTime().length() - 2));
            bookingShowBean.setUpdateTime(bookingShowBean.getUpdateTime().substring(0, bookingShowBean.getUpdateTime().length() - 2));
            bookingShowBean.setShowPicture(bookingShowBean.getShowPicture().split(";")[0]);
        });
        long count = backBookingService.countBooking(fromBean);
        int pageCount = (int) Math.ceil(((double) count / fromBean.getPageSize()));
        result.put("list", list);
        result.put("count", count);
        result.put("pageCount", pageCount);
        result.put("pageSize", fromBean.getPageSize());
        return result;
    }

    @RequestMapping("/saveBooking")
    public Map<String, Object> saveBooking(BookingBean bookingBean) {
        Map<String, Object> result = Maps.newHashMap();
        try {
            int status = backBookingService.saveBooking(bookingBean);
            if (status == 1) {
                result.put("status", status);
                result.put("msg", "添加成功");
            } else {
                result.put("status", 2);
                result.put("msg", "添加失败");
            }
            return result;
        } catch (Exception e) {
            result.put("status", 2);
            result.put("msg", "添加失败");
            logger.error("saveBooking error", e);
        }
        return result;
    }


    @RequestMapping("/modifyBooking")
    public Map<String, Object> modifyBooking(BackEndFromBean fromBean) {
        Map<String, Object> result = Maps.newHashMap();
        try {
            fromBean.setUpdateTime(new Date());
            int status = backBookingService.modifyBooking(fromBean);
            if (status == 1) {
                result.put("status", status);
                result.put("msg", "修改成功");
            } else {
                result.put("status", 2);
                result.put("msg", "修改失败");
            }
            return result;
        } catch (Exception e) {
            result.put("status", 2);
            result.put("msg", "修改失败");
            logger.error("modifyBooking error", e);
        }
        return result;
    }

    @RequestMapping("/getBookingById")
    public BookingShowBean getBookingById(long id){
        return backBookingService.getBookingById(id);
    }

}
